<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="1060px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="90px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="8">
          <el-row style="padding: 0 10px 0 0">
            <el-col :span="24">
              <el-form-item label="打卡地点" prop="addr">
                <el-input v-model="form.addr" size="small" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" size="small" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="位置" prop="position">
                {{form.position}}
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="16">
          <el-row style="padding: 0 10px 0 0">
            <el-col :span="24">
              <el-form ref="filter" :model="filter" label-width="0">
                <el-form-item class="mr10 mb5 fl">
                  <el-input v-model="filter.address" placeholder="检索" size="small" clearable />
                </el-form-item>
                <el-form-item class="mr10 mb5 fl" style="width: 300px">
                  <el-button type="primary" size="small" @click="handleInfo()">查 询</el-button>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="24">
              <div id="container" style="width: 620px; height: 400px"></div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPunch, updPunch } from '@/api/punch'
import { geocoder } from '@/api/tmap'
import { filter } from 'jszip/lib/object'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      map: false,
      position: {latitude: 31.30356407, longitude: 120.59241223},
      rules: {
        addr: [{ required: true, trigger: 'blur', message: '打卡地点不可为空' }]
      },
      filter: {},
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        if(this.form.latitude !== '' && this.form.longitude !== '') {
          this.position = this.form
        }
        this.$nextTick(() => {
          this.initTXDT()
        })
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    handleInfo() {
      geocoder({
        ...this.filter
      }).then(response => {
        this.position = response.info
        this.map.setCenter(new TMap.LatLng(this.position.latitude, this.position.longitude))
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    initTXDT() {
      let self = this
      let center = new TMap.LatLng(this.position.latitude, this.position.longitude)
      this.map = new TMap.Map("container", {
        rotation: 0,//设置地图旋转角度
        pitch:0, //设置俯仰角度（0~45）
        zoom:16,//设置地图缩放级别
        center: center//设置地图中心点坐标
      })
      let markerLayer = null
      if(this.form.latitude !== '' && this.form.longitude !== '') {
        markerLayer = new TMap.MultiMarker({
          id: 'marker-layer',
          map: self.map
        })
        markerLayer.add({
          position: new TMap.LatLng(self.form.latitude, self.form.longitude)
        })
      }
      this.map.on("click",function(evt){
        if(markerLayer) {
          markerLayer.setMap(null)
          markerLayer = null
        }
        let lat = evt.latLng.getLat().toFixed(6);
        let lng = evt.latLng.getLng().toFixed(6);
        markerLayer = new TMap.MultiMarker({
          id: 'marker-layer',
          map: self.map
        })
        markerLayer.add({
          position: evt.latLng
        })
        self.form.position = ''
        if(evt.poi && evt.poi.name) self.form.position = evt.poi.name
        self.form.latitude = lat
        self.form.longitude = lng
      })
    },
    handleEdit() {
      if(this.form.latitude === '' || this.form.longitude === '') {
        this.$message.error('请选择经纬度')
        return
      }
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addPunch(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updPunch(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
      this.map.destroy()
    }
  }
}
</script>

<style>

</style>
